package com.hyt.it.ogt.kq.main.controller.bm;


import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.bm.config.CopywritingConfig;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.service.bm.model.entity.Dict;
import com.hyt.it.ogt.kq.service.bm.service.IDictService;
import com.hyt.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-09-11
 */
@Api(tags = "25.数据字典", value = "数据字典")
@ApiSort(value = 25)
@RestController
@RequestMapping("/bm/dict")
public class DictController {

    private final static String OFFICE_DEFAULT = "default";

    @Autowired
    private IDictService dictService;
    @Resource
    CopywritingConfig copywritingConfig;

    @ApiOperation(value = "25.1.1根据id获取数据字典", notes = "20000: 成功；31251101：获取字典数据为空")
    @ApiOperationSort(value = 1)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id) {
        Dict dict = dictService.getById(id);
        if (dict != null) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(dict).build();
        } else {
            return ApiResponse.builder().code(31251101).build();
        }
    }

    @ApiOperationSort(value = 2)
    @ApiOperation(value = "25.1.2根据类型查询数据字典", notes = "20000: 成功")
    @GetMapping("getByType")
    public Object getByType(@RequestParam String type) {
        List<Dict> dicts = dictService.getByType(type);
        return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(dicts).build();
    }


    @ApiOperationSort(value = 10)
    @ApiOperation(value = "25.2.1添加数据字典", notes = "20000: 成功；31252101：保存字典数据为空")
    @Log(module = "数据字典管理", business = "添加数据字典")
    @PostMapping("add")
    public Object add(@RequestBody Dict dict) {
        Boolean flag = dictService.save(dict);
        if (flag) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        } else {
            return ApiResponse.builder().code(31252101).build();
        }
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "25.3.1更新数据字典", notes = "20000: 成功；31253101：更新字典数据为空")
    @Log(module = "数据字典管理", business = "更新数据字典")
    @PutMapping("update")
    public Object update(@RequestBody Dict dict) {
        boolean flag = dictService.updateById(dict);
        if (flag) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        } else {
            return ApiResponse.builder().code(31253101).build();
        }
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "25.4.1根据id删除数据字典", notes = "20000: 成功；31254101：删除字典数据为空")
    @Log(module = "数据字典管理", business = "删除数据字典")
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id) {
        boolean flag = dictService.removeById(id);
        if (flag) {
            return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).build();
        } else {
            return ApiResponse.builder().code(31254101).build();
        }
    }

    @ApiOperationSort(value = 31)
    @ApiOperation(value = "25.5.1 文案描述", notes = "20000: 成功；31254101：删除字典数据为空")
    @ThrowsAdvice(code = 31250501, print = "文案描述拉取异常")
    @GetMapping("copywriting")
    public ApiResponse<Map<String, String>> copywriting(@RequestParam(defaultValue = OFFICE_DEFAULT) String officeId) {
        Map<String, String> control = copywritingConfig.getControl();
        String officeControl = MapUtil.getStr(control, officeId, OFFICE_DEFAULT);
        
        Map<String, Map<String, String>> copywriting = copywritingConfig.getDescription();
        Map<String, String> officeCopyWriting = MapUtil.get(copywriting, officeControl, new TypeReference<Map<String, String>>() {
        });

        return ApiResponse.<Map<String, String>>builder()
                .data(officeCopyWriting)
                .code(Constant.RESPONSE_CODE_SUCCESS)
                .build();
    }
}
